package com.sbp.message.schedule;

import com.dangdang.ddframe.job.api.ShardingContext;
import com.dangdang.ddframe.job.api.simple.SimpleJob;
import com.dap.dao.DaoSupport;
import com.dap.utils.SpringUtils;
import com.sbp.message.entity.ProducerMessageRecord;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.InitializingBean;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by wangmin on 2018/4/28.
 */
public class HistoryProducerRecordCheckSchedule implements SimpleJob, InitializingBean {
    private static final Logger logger = LoggerFactory.getLogger(HistoryProducerRecordCheckSchedule.class);

    private DaoSupport daoService;
    @Override
    public void afterPropertiesSet() throws Exception {
        daoService = SpringUtils.getBean("daoService");
    }


    @Override
    public void execute(ShardingContext shardingContext) {
        final long day180Millis = 180 * 24*3600*1000L;
        Date date = new Date(System.currentTimeMillis() - day180Millis);

        Map<String, Object> map = new HashMap();
        map.put("oldest_time", date);
        int deleteRowCount = daoService.delete(ProducerMessageRecord.class, "deleteRecordBeforeTime", map);

        logger.info("delete {} row before {}", deleteRowCount, date.toString());
    }

}
